﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.Util;
using Com.CMS263.PO;
using Com.CMS263.DAO;
namespace Com.CMS263.BLL
{
    public class ArtTypeBLL
    {
        ArtTypeDAO typeDAO = new ArtTypeDAO();
        AttributeTypeBLL atbll = new AttributeTypeBLL();
        public bool Add(ArtType type)
        {

            if (string.IsNullOrEmpty(type.typeName))
            {
                StringUtil.Alert("请填写标题", "-1");
                return false;
            }

            typeDAO.Add(type);
            return true;
        }
        public bool Modify(ArtType type)
        {
            if (string.IsNullOrEmpty(type.typeName))
            {
                StringUtil.Alert("请填写标题", "-1");
                return false;
            }
            typeDAO.Modify(type);
            return true;
        }
        public IList<ArtType> GetAllTypeByIshow(int isShow)
        {
            return typeDAO.GetAllTypeByIshow(isShow);
        }
        public IList<ArtType> GetAllType(int isShow)
        {
            return typeDAO.GetAllType();
        }

        public IList<ArtType> GetArtTypeByPid(int pid)
        {
            return typeDAO.GetArtTypeByPid(pid);
        }

        public void DeleteTypeById(int id)
        {
            typeDAO.Delete(id);
        }
        /// <summary>
        /// 得到某类的所有子类
        /// </summary>
        /// <param name="pid">类别编号</param>
        /// <param name="p">是否显示1为所有显示的0为所有不显示的，用于回收站</param>
        /// <returns></returns>
        public IList<ArtType> GetArtTypeByPid(int pid, int p)
        {
            return typeDAO.GetArtTypeByPid(pid, p);
        }
        /// <summary>
        /// 通过编号得到一个分类
        /// </summary>
        /// <param name="pid">分类编号</param>
        /// <returns></returns>
        public ArtType GetArtTypeById(int pid)
        {
            return typeDAO.GetArtTypeById(pid);
        }
        /// <summary>
        /// 得到树形结构的菜单
        /// </summary>
        /// <returns></returns>
        public string GetTypeTree()
        {

            StringBuilder sb = new StringBuilder("<ul  >");
            IList<ArtType> list = GetArtTypeByPid(0);
           
            //循环生成菜单列表
            foreach (ArtType at in list)
            {

                //如果这个菜单的外部程序连接编号为0，那就找父类的外部程序连接
                string outApp = "";
                if (at.outLink != 0)
                {
                    AttributeType attrType = atbll.GetAttributeById(at.outLink);
                    outApp = attrType.outApp;
                    if (outApp.IndexOf("?") > 0)
                    {
                        outApp += "&pid=" + attrType.id + "&menuId=" + at.id;
                    }
                    else
                    {
                        outApp += "?pid=" + attrType.id + "&menuId=" + at.id;
                    }
                }



                //大类是没有连接的，只是一个标题
                if (this.GetArtTypeByPid(at.id).Count > 0)
                {
                    sb.Append("<li ><a title='" + at.typeName + "'  id='" + at.id + "'   >" + StringUtil.GetText(at.typeName, 20) + "</a>");

                    sb.Append(GetTree(at.id, 0));
                }
                else
                {
                    sb.Append("<li ><a title='" + at.typeName + "'  id='" + at.id + "'  >" + StringUtil.GetText(at.typeName, 20) + "</a>");

                    sb.Append("<ul><li  class='Child'><a  title='" + at.typeName + "' href='" + outApp + "' target='mainFrame'  id='" + at.id + "' >" + at.typeName + "</a></li></ul>");
                }
                sb.Append("</li>");
            }

            sb.Append("</ul>");

            return sb.ToString();
        }
        public string GetTree(int Pid, int level)
        {

            StringBuilder sb = new StringBuilder("");
            IList<ArtType> list = GetArtTypeByPid(Pid);
            ArtType subType = this.GetArtTypeById(Pid);

            level++;

            //递归生成菜单
            //根据父类编号取得子类列表

            if (list.Count > 0)
            {
                sb.Append("<ul >");
                foreach (ArtType at in list)
                {

                    string outApp = "";
                    AttributeType attrType = new AttributeType();
                    if (at.outLink != 0)
                    {
                        attrType = atbll.GetAttributeById(at.outLink);
                        if (attrType != null)
                        {

                            outApp = attrType.outApp;

                        }

                    }
                    else
                    {
                        //查找父类的外部程序连接
                        ArtType pType = typeDAO.GetArtTypeById(at.pid);
                        if (pType != null)
                        {
                            if (pType.outLink == 0)
                            {
                                ArtType gType = typeDAO.GetArtTypeById(pType.pid);
                                if (gType != null)
                                {
                                    attrType = atbll.GetAttributeById(gType.outLink);
                                }
                            }
                            else
                            {
                                attrType = atbll.GetAttributeById(pType.outLink);
                            }
                        }


                        if (attrType != null)
                        {

                            outApp = attrType.outApp;
                        }
                    }
                    if (outApp != null && outApp.IndexOf("?") > 0)
                    {
                        if (attrType != null)
                        {
                            outApp += "&pid=" + attrType.id + "&menuId=" + at.id;
                        }
                    }
                    else
                    {
                        if (attrType != null)
                        {
                            outApp += "?pid=" + attrType.id + "&menuId=" + at.id;
                        }
                    }

                    //判断外部程序情况
                    if (GetArtTypeByPid(at.id).Count > 0)
                    {
                        sb.Append("<li  id='m" + at.id + "'>");

                        sb.Append("<a title='" + at.typeName + "'  id='" + at.id + "' >" + StringUtil.GetText(at.typeName, 20) + "</a>");
                    }
                    else
                    {
                        sb.Append("<li  class='Child'  id='m" + at.id + "'>");

                        sb.Append("<a  href='" + outApp + "' title='" + at.typeName + "' target='mainFrame'  id='" + at.id + "' >" + StringUtil.GetText(at.typeName, 20) + "</a>");
                    }
                    sb.Append(GetTree(at.id, level));


                    sb.Append("</li>");
                }
                sb.Append("</ul>");

            }


            return sb.ToString();
        }


        /// <summary>
        /// 根据父分类得到
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public string GetMenuTree(int pid, string link)
        {
            StringBuilder sb = new StringBuilder("<ul >");
            IList<ArtType> list = GetArtTypeByPid(pid, 1);
            //循环生成菜单列表
            foreach (ArtType at in list)
            {

                //大类是没有连接的，只是一个标题
                if (this.GetArtTypeByPid(at.id).Count > 0)
                {
                    sb.Append("<li id='m" + at.id + "' ><a title='" + at.typeName + "' id='" + at.id + "' >" + at.typeName + "</a>");

                    sb.Append(GetMenu(at.id, pid, link));
                }
                else
                {
                    sb.Append("<li  class='Child' id='m" + at.id + "' ><a  href='" + link + ".aspx?mid=" + at.id + "&pid=" + pid + "' title='" + at.typeName + "' id='" + at.id + "' >" + at.typeName + "</a>");

                }
                sb.Append("</li>");
            }

            sb.Append("</ul>");

            return sb.ToString();
        }
        /// <summary>
        /// 递归生成子菜单
        /// </summary>
        /// <param name="Pid"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public string GetMenu(int Pid, int root, string link)
        {

            StringBuilder sb = new StringBuilder("");
            IList<ArtType> list = GetArtTypeByPid(Pid);
            ArtType subType = this.GetArtTypeById(Pid);
            


            //递归生成菜单
            //根据父类编号取得子类列表

            if (list.Count > 0)
            {
                sb.Append("<ul id='m" + Pid + "'>");
                foreach (ArtType at in list)
                {





                    //判断外部程序情况
                    if (GetArtTypeByPid(at.id).Count > 0)
                    {
                        sb.Append("<li  id='m" + at.id + "' >");
                        sb.Append("<a  style='cursor:pointer' title='" + at.typeName + "'  id='" + at.id + "'  >" + at.typeName + "</a>");
                    }
                    else
                    {

                        sb.Append("<li class='Child'  id='m" + at.id + "' >");
                        sb.Append("<a href='" + link + ".aspx?mid=" + at.id + "&pid=" + root + "' title='" + at.typeName + "'  id='" + at.id + "' >" + at.typeName + "</a>");

                    }
                    sb.Append(GetTree(at.id, root));


                    sb.Append("</li>");
                }
                sb.Append("</ul>");

            }


            return sb.ToString();
        }
    }
}
